package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.BspsMchntKeyInfoMapper;
import cc.rengu.igas.bsps.common.entity.MchntKeyInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户密钥信息表操作接口实现类
 * Created by 王成 on 2020/3/25
 */
public class BspsMchntKeyInfoMapperImpl implements BspsMchntKeyInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertBspsMchntKeyInfo(MchntKeyInfo mchntKeyInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertBspsMchntKeyInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntKeyInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户密钥信息失败,instId:<{}>,mchntNo:<{}>,appId:<{}>,keyType:<{}>,keyValue:<{}>,iReturnCode:<{}>",
                    mchntKeyInfo.getInstId(), mchntKeyInfo.getMchntNo(), mchntKeyInfo.getMchntAppId(), mchntKeyInfo.getKeyType(), mchntKeyInfo.getKeyValue(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateBspsMchntKeyInfoById(MchntKeyInfo mchntKeyInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBspsMchntKeyInfoById");
        mchntKeyInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntKeyInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户密钥信息失败,instId:<{}>,mchntNo:<{}>,appId:<{}>,keyType:<{}>,keyValue:<{}>,iReturnCode:<{}>",
                    mchntKeyInfo.getInstId(), mchntKeyInfo.getMchntNo(), mchntKeyInfo.getMchntAppId(), mchntKeyInfo.getKeyType(), mchntKeyInfo.getKeyValue(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public MchntKeyInfo selectBspsMchntKeyInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntKeyInfoById");
        MchntKeyInfo mchntKeyInfo = new MchntKeyInfo();
        mchntKeyInfo.setInstId(instId);
        mchntKeyInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntKeyInfo, mchntKeyInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户密钥信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return mchntKeyInfo;
    }

    @Override
    public MchntKeyInfo selectBspsMchntKeyInfoByPrimaryKey(String instId, String mchntNo, String appId, String keyType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntKeyInfoByPrimaryKey");
        MchntKeyInfo mchntKeyInfo = new MchntKeyInfo();
        mchntKeyInfo.setInstId(instId);
        mchntKeyInfo.setMchntNo(mchntNo);
        mchntKeyInfo.setMchntAppId(appId);
        mchntKeyInfo.setKeyType(keyType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntKeyInfo, mchntKeyInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户密钥信息失败,instId:<{}>,mchntNo:<{}>,appId:<{}>,keyType:<{}>,iReturnCode:<{}>", instId, mchntNo, appId, keyType, iReturnCode);
            return null;
        }
        return mchntKeyInfo;
    }

    @Override
    public List<MchntKeyInfo> selectBspsMchntKeyInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntKeyInfoByMchntNo");
        MchntKeyInfo mchntKeyInfo = new MchntKeyInfo();
        mchntKeyInfo.setInstId(instId);
        mchntKeyInfo.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntKeyInfo, MchntKeyInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户号查询商户密钥信息失败,instId:<{}>,mchntNo:<{}>,mchntName:<{}>", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntKeyInfo) item).collect(Collectors.toList());
    }
}
